package com.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.common.utils.PageUtils;
import com.common.utils.Query;
import com.dao.ExamManageDao;
import com.entity.*;
import com.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("examManageService")
public class ExamManageServiceImpl<T> extends ServiceImpl<ExamManageDao, ExamManageEntity> implements ExamManageService {

    @Autowired
    private ExamManageDao examManageDao;
    @Autowired
    private PaperManageService paperManageService;

    @Autowired
    private MultiQuestionService multiQuestionService;//选择题

    @Autowired
    private FillQuestionService fillQuestionService;//填空题

    @Autowired
    private JudgeQuestionService judgeQuestionService;//判断题

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ExamManageEntity> page = this.page(
                new Query<ExamManageEntity>().getPage(params),
                new QueryWrapper<ExamManageEntity>()
                        .or().like("examcode",params.get("key"))
                        .or().like("source",params.get("key"))
                        .or().like("paperid",params.get("key"))
                        .or().like("examdate",params.get("key"))
                        .or().like("major",params.get("key"))
                        .or().like("institute",params.get("key"))
                        .or().like("type",params.get("key"))

        );

        return new PageUtils(page);
    }

    @Override
    public List<ExamManageEntity> getAllList() {
        return examManageDao.selectList(new QueryWrapper<>());
    }

    //产生新的试卷
    @Override
    public boolean createPaper(Integer paperid,String source) {
        List<MultiQuestionEntity> multiAllList = multiQuestionService.getAllList();//20个选择题
        List<FillQuestionEntity> fillAllList = fillQuestionService.getAllList();//15个填空题
        List<JudgeQuestionEntity> judgeAllList = judgeQuestionService.getAllList();//15个判断题
        paperManageService.removeByPaperId(paperid);

        List<PaperManageEntity> list=new ArrayList<>();

        List<PaperManageEntity> multi = createMulti(multiAllList, source, paperid);
        List<PaperManageEntity> fill = createFill(fillAllList, source, paperid);
        List<PaperManageEntity> judge = createJudge(judgeAllList, source, paperid);
        if (multi!=null){
            list.addAll(multi);
        }
        if (fill!=null){
            list.addAll(fill);
        }
        if(judge!=null){
            list.addAll(judge);
        }
        if (list.size()==0){
            return false;
        }
        for (PaperManageEntity paperManageEntity : list) {
            paperManageService.save(paperManageEntity);
        }
        return true;
    }
    public List<PaperManageEntity> createMulti(List<MultiQuestionEntity> list,String source,Integer paperid){
        List<PaperManageEntity> paperList=new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getSubject().equals(source)){
                PaperManageEntity p=new PaperManageEntity();
                p.setPaperid(paperid);
                p.setQuestionid(list.get(i).getQuestionid());
                p.setQuestiontype(1);
                paperList.add(p);
            }
        }
        return paperList;
    }
    public List<PaperManageEntity> createFill(List<FillQuestionEntity> list,String source,Integer paperid){
        List<PaperManageEntity> paperList=new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getSubject().equals(source)){
                PaperManageEntity p=new PaperManageEntity();
                p.setPaperid(paperid);
                p.setQuestionid(list.get(i).getQuestionid());
                p.setQuestiontype(2);
                paperList.add(p);
            }
        }
        return paperList;
    }

    public List<PaperManageEntity> createJudge(List<JudgeQuestionEntity> list,String source,Integer paperid){
        List<PaperManageEntity> paperList=new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getSubject().equals(source)){
                PaperManageEntity p=new PaperManageEntity();
                p.setPaperid(paperid);
                p.setQuestionid(list.get(i).getQuestionid());
                p.setQuestiontype(3);
                paperList.add(p);
            }
        }
        return paperList;
    }

    public static void main(String[] args) {
        for (int i=0;i<100;i++)
            System.out.println(randomId(5 ));
    }
    private static int  randomId(Integer n){

        Double d = Math.random();
        d*=n;
        return  (int) Math.round(d);
    }
}